package com.rui.pool;

import java.util.concurrent.*;

public class Demo01 {
    public static void main(String[] args) {

        //单个线程
//        ExecutorService threadPool = Executors.newSingleThreadExecutor();

        //固定线程大小
//        ExecutorService threadPool = Executors.newFixedThreadPool(5);

        //可伸缩的，遇强则强，遇弱则弱
//        ExecutorService threadPool = Executors.newCachedThreadPool();

        ExecutorService threadPool = new ThreadPoolExecutor(2,
                5,
                3,
                TimeUnit.SECONDS,
                new LinkedBlockingDeque<>(3),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy()
        );

        try {
            for (int i = 0; i < 6; i++) {
                threadPool.execute(() -> {
                    System.out.println(Thread.currentThread().getName() + " OK");
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //线程池用完要关闭的
            threadPool.shutdown();
        }

//        //创建一个固定的线程池的大小
//        Executors.newFixedThreadPool(5);
//
//        //可以伸缩的
//        Executors.newCachedThreadPool();

        //创建十个线程
//        ExecutorService threadPool = Executors.newFixedThreadPool(10);
    }
}
